package info.magnolia.blossom.sample.cv;

import info.magnolia.blossom.sample.cv.model.CurriculumVitae;
import info.magnolia.blossom.sample.cv.service.CVService;
import info.magnolia.blossom.sample.cv.util.ApplicationUtil;
import info.magnolia.blossom.sample.cv.util.WebUtil;
import info.magnolia.module.blossom.annotation.TabFactory;
import info.magnolia.module.blossom.annotation.Template;
import info.magnolia.module.blossom.annotation.TemplateDescription;
import info.magnolia.module.blossom.dialog.TabBuilder;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.InitBinder;


import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: valentinzuld
 * Date: 2/26/13
 * Time: 9:52 AM
 * To change this template use File | Settings | File Templates.
 */
@Template(title = "Added CV", id = "blossomSample:components/cvadded")
@TemplateDescription("Display Added CV")
@Controller
public class CvAdded {

    /**
     * Class logger.
     */
    private static final Logger logger = Logger.getLogger(CvAdded.class);

    @Autowired
    private CVService cvService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        CustomDateEditor editor = new CustomDateEditor(new SimpleDateFormat(ApplicationUtil.DATE_PATTERN), true);
        binder.registerCustomEditor(Date.class, editor);
    }

    @RequestMapping(value = "/cv-added.html", method = RequestMethod.POST)
    public String show(final ModelMap model, @ModelAttribute CurriculumVitae cv, final HttpServletRequest request){

        if (StringUtils.isBlank(cv.getName())) {
            logger.warn("No name specified for CV");
            model.put(WebUtil.ERROR_MSG, "The name is mandatory on the CV");
            return WebUtil.ERROR_PAGE_PATH;
        }

        cv.setPictureFile(WebUtil.getFileContent(request, WebUtil.PARAM_UPLOAD_PICTURE_PARAM));
        cv.setPictureFileName(WebUtil.getFileName(request, WebUtil.PARAM_UPLOAD_PICTURE_PARAM));

        try {
            cvService.addCV(cv);
            model.put("cv", cv);
        } catch (Exception e) {
            logger.error("Error adding a new CV", e);
            model.put(WebUtil.ERROR_MSG, "There was an error adding the CV:</br>" + e.getMessage());
            return WebUtil.ERROR_PAGE_PATH;
        }

        return "components/cvAdded.jsp";
    }

    @TabFactory("Settings")
    public void settingsTab(final TabBuilder tab) {
        tab.addUuidLink("viewLink", "View Link", "Link to the view page");
        tab.addUuidLink("addLink", "Add New Link", "Link to the Add New page");
        tab.addUuidLink("listLink", "List Link", "Link to the View All CVs page");
    }
}
